package top.lyc829.zzl_android.ui.thing.follow;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import java.io.IOException;
import java.util.List;

import okhttp3.Response;
import top.lyc829.zzl_android.logic.bean.DynamicBean;
import top.lyc829.zzl_android.logic.bean.ResultBean;
import top.lyc829.zzl_android.logic.network.Network;
import top.lyc829.zzl_android.logic.util.HttpUtil;
import top.lyc829.zzl_android.logic.util.Utils;
import top.lyc829.zzl_android.logic.util.gson.JsonUtilNew;

public class FellowViewModel extends ViewModel {
    private MutableLiveData<List<DynamicBean>> dynamicListLiveData;

    private MutableLiveData<Integer> deletePositionLiveData;

    public MutableLiveData<Integer> getDeletePositionLiveData() {
        if (deletePositionLiveData == null) {
            deletePositionLiveData = new MutableLiveData<>();
            deletePositionLiveData.setValue(-1);
        }
        return deletePositionLiveData;
    }

    public MutableLiveData<List<DynamicBean>> getDynamicLiveData() {
        if (dynamicListLiveData == null) {
            dynamicListLiveData = new MutableLiveData<>();
        }
        return dynamicListLiveData;
    }


    /**
     * 动态页面获取动态
     * @param page 分页
     */
    public void getDynamics(int page, int userId) {
        Network.getFellowDynamics(page, userId, new HttpUtil.MyCallback() {
            @Override
            public void success(Response res) throws IOException {

                String resultStr = res.body().string();

                ResultBean<List<DynamicBean>> resultBean = JsonUtilNew.fromJsonArray(resultStr, DynamicBean.class);
                if (resultBean.getCode().equals(200)) {
                    getDynamicLiveData().postValue(resultBean.getData());
                } else {
                    failed(new IOException(resultBean.getMsg()));
                }
            }

            @Override
            public void failed(IOException e) {
                Utils.Sout(e.getMessage());
            }
        });
    }
}